<?php

/**
 * Impress - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Impress
 * @package Impress
 */

/**
 * This class provides access to data layer.
 *
 * @package Impress
 * @subpackage Models
 */

class ADS_DataFrameItemModel extends ADS_DefaultModel {
    
    public function countItems($frameId) {
        if(!is_int($frameId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT
            COUNT(*) as cnt
          FROM
            [:wp:ads_dataframe_items] AS t1
          INNER JOIN
            [:wp:ads_datasources] AS t2 ON t1.sourceId = t2.sourceId
          WHERE
            t1.frameId = %i', $frameId, '
            %if', (bool)$this->useFilter, 'AND %and', $this->filter, '
          ORDER BY
            t1.position
        ')->cnt;
    }
    
    public function getItems($frameId) {
        if(!is_int($frameId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetchAll('
          SELECT
            t1.itemId,
            t1.position,
            t1.validFrom,
            t1.validTo,
            t1.sourceId,
            t2.name,
            t2.content
          FROM
            [:wp:ads_dataframe_items] AS t1
          INNER JOIN
            [:wp:ads_datasources] AS t2 ON t1.sourceId = t2.sourceId
          WHERE
            t1.frameId = %i', $frameId, 
          '%if', (bool)$this->useFilter, 'AND %and', $this->filter, '%end',
          '%if', (bool)$this->useOrderBy, 'ORDER BY %by', $this->orderBy, '%end',
          '%if', (bool)$this->useLimit, 'LIMIT %i OFFSET %i', $this->limit, $this->offset
        );
    }
    
    public function getItem($itemId) {
        if(!is_int($itemId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT
            t1.itemId,
            t1.position,
            t1.validFrom,
            t1.validTo,
            t1.sourceId,
            t2.name
          FROM
            [:wp:ads_dataframe_items] AS t1
          INNER JOIN
            [:wp:ads_datasources] AS t2 ON t1.sourceId = t2.sourceId
          WHERE
            t1.itemId = %i', $itemId
        );
    }
    
    public function addItem($item){
        if(!is_array($item)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //format date
        $item['validFrom%t'] = $item['validFrom'];unset($item['validFrom']);
        $item['validTo%t']   = $item['validTo'];unset($item['validTo']);
        //get next position
        $item['position'] = ((int)dibi::fetch('
            SELECT 
                MAX(position) as position 
            FROM 
                [:wp:ads_dataframe_items] 
            WHERE 
                frameId = %i 
        ', $item['frameId'])->position)+1;
        //insert
        dibi::query('INSERT INTO [:wp:ads_dataframe_items]', $item);
    }
    
    public function removeItem($itemId){        
        if(!is_int($itemId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //get item
        $item = dibi::fetch('SELECT * FROM [:wp:ads_dataframe_items] WHERE itemId = %i',$itemId);
        //update positions
        dibi::query('
            UPDATE 
                [:wp:ads_dataframe_items] 
            SET 
                position = position - 1 
            WHERE 
                frameId = %i 
            AND 
                position > %i
        ', $item->frameId, $item->position);
        //remove item
        dibi::query('DELETE FROM [:wp:ads_dataframe_items] WHERE itemId = %i LIMIT 1', $itemId);
    }
    
    public function increaseItemPosition($itemId){        
        if(!is_int($itemId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //get item
        $item = dibi::fetch('SELECT * FROM [:wp:ads_dataframe_items] WHERE itemId = %i',$itemId);
        //update item on next position
        dibi::query('
            UPDATE 
                [:wp:ads_dataframe_items] 
            SET 
                position = position - 1 
            WHERE 
                frameId = %i 
            AND 
                position = %i + 1
        ', $item->frameId, $item->position);
        //update item
        dibi::query('
            UPDATE 
                [:wp:ads_dataframe_items] 
            SET 
                position = position + 1 
            WHERE 
                itemId = %i 
        ', $itemId);
    }
    
    public function decreaseItemPosition($itemId){        
        if(!is_int($itemId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //get item
        $item = dibi::fetch('SELECT * FROM [:wp:ads_dataframe_items] WHERE itemId = %i',$itemId);
        //update item on next position
        dibi::query('
            UPDATE 
                [:wp:ads_dataframe_items] 
            SET 
                position = position + 1 
            WHERE 
                frameId = %i 
            AND 
                position = %i - 1
        ', $item->frameId, $item->position);
        //update item
        dibi::query('
            UPDATE 
                [:wp:ads_dataframe_items] 
            SET 
                position = position - 1 
            WHERE 
                itemId = %i 
        ', $itemId);
    }
    
    public function editItem($itemId, $item){        
        if(!is_int($itemId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        dibi::query('UPDATE [:wp:ads_dataframe_items] SET', $item, 'WHERE itemId = %i', $itemId);
    }

}
